package com.ocss.system.service.impl;

import java.util.List;

import com.ocss.common.core.domain.entity.SysUser;
import com.ocss.common.utils.DateUtils;
import com.ocss.common.utils.SecurityUtils;
import com.ocss.system.domain.OcssStudent;
import com.ocss.system.mapper.SysUserMapper;
import com.ocss.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ocss.system.mapper.OcssTeacherMapper;
import com.ocss.system.domain.OcssTeacher;
import com.ocss.system.service.IOcssTeacherService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 教师信息Service业务层处理
 *
 * @author ligen
 * @date 2024-03-06
 */
@Service
public class OcssTeacherServiceImpl implements IOcssTeacherService {
    @Autowired
    private OcssTeacherMapper ocssTeacherMapper;

    @Resource
    private OcssStudentServiceImpl ocssStudentService;

    @Resource
    private ISysConfigService configService;

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private SysUserServiceImpl sysUserService;

    /**
     * 查询教师信息
     *
     * @param id 教师信息主键
     * @return 教师信息
     */
    @Override
    public OcssTeacher selectOcssTeacherById(Long id) {
        return ocssTeacherMapper.selectOcssTeacherById(id);
    }

    /**
     * 查询教师信息列表
     *
     * @param ocssTeacher 教师信息
     * @return 教师信息
     */
    @Override
    public List<OcssTeacher> selectOcssTeacherList(OcssTeacher ocssTeacher) {
        return ocssTeacherMapper.selectOcssTeacherList(ocssTeacher);
    }

    /**
     * 新增教师信息
     *
     * @param ocssTeacher 教师信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOcssTeacher(OcssTeacher ocssTeacher) {
        ocssTeacher.setCreateTime(DateUtils.getNowDate());

        // 判断该用户的手机号是否重复
        String p = ocssTeacher.getPhone();
        OcssTeacher ocssTeacher1 = new OcssTeacher();
        ocssTeacher1.setPhone(p);
        List<OcssTeacher> ocssTeachers = ocssTeacherMapper.selectOcssTeacherList(ocssTeacher1);
        if (!ocssTeachers.isEmpty()) {
            return -1;
        }

        // 根据身份证信息计算年龄和性别，同时将教师添加到用户表中，并默认分配教师的角色，教师默认密码设置为身份证后六位+系统密码参数
        String idCard = ocssTeacher.getIdCard();

        ocssTeacher.setAge(ocssStudentService.calculateAge(idCard));
        ocssTeacher.setSex(ocssStudentService.getGender(idCard));
        // 身份证后六位
        String lastSixDigits = idCard.substring(idCard.length() - 6);
        // 系统初始化密码  这里需要将密码和身份证后六位进行拼接后加密
        String initPassword = configService.selectConfigByKey("sys.user.initPassword");
        String password = SecurityUtils.encryptPassword(lastSixDigits + initPassword);

        // 将教师对象组装为用户对象
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(ocssTeacher.getDeptId());
        sysUser.setUserName(ocssTeacher.getPhone());
        sysUser.setNickName(ocssTeacher.getName());
        sysUser.setPhonenumber(ocssTeacher.getPhone());
        sysUser.setSex(ocssTeacher.getSex());
        sysUser.setPassword(password);
        sysUser.setRoleIds(new Long[]{100L});
        // 设置用户权限为教师
        int row = ocssTeacherMapper.insertOcssTeacher(ocssTeacher);
        sysUser.setPid(ocssTeacher.getId());
        userMapper.insertUser(sysUser);
        sysUserService.insertUserRole(sysUser);

        return row;
    }

    /**
     * 修改教师信息
     *
     * @param ocssTeacher 教师信息
     * @return 结果
     */
    @Override
    public int updateOcssTeacher(OcssTeacher ocssTeacher) {
        ocssTeacher.setUpdateTime(DateUtils.getNowDate());
        return ocssTeacherMapper.updateOcssTeacher(ocssTeacher);
    }

    /**
     * 批量删除教师信息
     *
     * @param ids 需要删除的教师信息主键
     * @return 结果
     */
    @Override
    public int deleteOcssTeacherByIds(Long[] ids) {
        return ocssTeacherMapper.deleteOcssTeacherByIds(ids);
    }

    /**
     * 删除教师信息信息
     *
     * @param id 教师信息主键
     * @return 结果
     */
    @Override
    public int deleteOcssTeacherById(Long id) {
        return ocssTeacherMapper.deleteOcssTeacherById(id);
    }
}
